package com.dyna.cp.elasticsearch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dyna.constants.EsIndicesConstant;
import com.dyna.cp.elasticsearch.domain.DynaEsDataVo;
import com.dyna.cp.elasticsearch.domain.entity.Dyna;
import com.dyna.cp.elasticsearch.domain.param.DynaParam;
import com.dyna.cp.elasticsearch.service.DynaEsService;
import com.dyna.domain.resp.PageResult;
import com.dyna.resp.GroupResp;
import com.dyna.utils.EsUtil;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhoucaiwang
 */
@Service
public class DynaEsServiceImpl implements DynaEsService {

    @Override
    public List<String> getIndices() {
        return EsUtil.getIndices();
    }

    @Override
    public Boolean addIndex(String index) {
        return EsUtil.addIndex(index);
    }

    @Override
    public Boolean delIndex(String index) {
        return EsUtil.delIndex(index);
    }

    @Override
    public Boolean isExist(String index) {
        return EsUtil.isExist(index);
    }

    @Override
    public String addData(String index, Map<String, Object> dyna) {
        return EsUtil.addData(index,dyna);
    }

    @Override
    public Boolean addBatchData(String index,List<Dyna> data) {
        EsUtil.addBatchData(index,data);
        return Boolean.TRUE;
    }

    @Override
    public PageResult<?> queryDataAll(String index) {
        return EsUtil.queryDataAll(index, DynaEsDataVo.class);
    }

    @Override
    public PageResult<?> queryDataPage(String index, DynaParam dynaParam) {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery());
        return EsUtil.queryDataPage(index, builder,dynaParam.getSortField(), SortOrder.DESC);
    }

    @Override
    public Object queryDataById(String index, String id) {
        return EsUtil.queryDataById(index,id, JSONObject.class);
    }

    @Override
    public Boolean updateData(String index,String id,Dyna dyna) {
        return EsUtil.updateData(index,id,dyna);
    }

    @Override
    public Boolean deleteData(String index,String id) {
        return EsUtil.delData(index,id);
    }

    @Override
    public List<Map<String, Object>> group(String index, String field,String type) {
        return EsUtil.group(index, field, type);
    }

    @Override
    public Double getScoreStats(String index, String field, Boolean type) {
        return EsUtil.stats(index, field, type);
    }

    @Override
    public List<GroupResp> getGroupStats(String index, String groupField, String staticField, Boolean type, String sort) {
        return EsUtil.getGroupStats(index, groupField, staticField, type, sort);
    }

    @Override
    public Map<String, Object> getGatewayLogByTraceId(String traceId) {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("traceId", traceId);
        builder.query(matchQueryBuilder);
        Map<String, Object> map = EsUtil.queryOne(EsIndicesConstant.GATEWAY_LOG_INDEX, builder, "startTime.keyword", SortOrder.DESC);
        return map;
    }


}
